package cn.iocoder.yudao.module.kt.service.studyfile;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.kt.controller.admin.studyfile.vo.*;
import cn.iocoder.yudao.module.kt.dal.dataobject.studyfile.StudyFileDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.kt.dal.mysql.studyfile.StudyFileMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.kt.enums.ErrorCodeConstants.*;

/**
 * 学习资料 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StudyFileServiceImpl implements StudyFileService {

    @Resource
    private StudyFileMapper studyFileMapper;

    @Override
    public Long createStudyFile(StudyFileSaveReqVO createReqVO) {
        // 插入
        StudyFileDO studyFile = BeanUtils.toBean(createReqVO, StudyFileDO.class);
        studyFileMapper.insert(studyFile);
        // 返回
        return studyFile.getId();
    }

    @Override
    public void updateStudyFile(StudyFileSaveReqVO updateReqVO) {
        // 校验存在
        validateStudyFileExists(updateReqVO.getId());
        // 更新
        StudyFileDO updateObj = BeanUtils.toBean(updateReqVO, StudyFileDO.class);
        studyFileMapper.updateById(updateObj);
    }

    @Override
    public void deleteStudyFile(Long id) {
        // 校验存在
        validateStudyFileExists(id);
        // 删除
        studyFileMapper.deleteById(id);
    }

    private void validateStudyFileExists(Long id) {
        if (studyFileMapper.selectById(id) == null) {
            throw exception(STUDY_FILE_NOT_EXISTS);
        }
    }

    @Override
    public StudyFileDO getStudyFile(Long id) {
        return studyFileMapper.selectById(id);
    }

    @Override
    public PageResult<StudyFileDO> getStudyFilePage(StudyFilePageReqVO pageReqVO) {
        return studyFileMapper.selectPage(pageReqVO);
    }

}